<template>
  <div class="content">
    <jc-button
      :btn-arr="['reload', 'newy', 'remove', 'pass', 'sub', 'option']"
      :disab-arr="['close', 'using', 'cancel', 'copy', 'freeze', 'com1', 'com2', 'chart']"
      @refresh="handleGetUntreated"
      @newly="addMateril"
      @audit="Retrial(1)"
      @noAudit="Retrial(2)"
      @submit="materSub(1)"
      @noSubmit="materSub(2)"
      @del="delCustom"
      @toExcel="handleExport"
    />
    <div class="header">
      <div class="header-name">
        <search :options="selectData" :search-son="searchSon" :msg="tacitly" @seek="collect" @hand="handleQuerySonClass" />
        <el-button type="primary" class="btn" size="mini" @click="handleQuerySonClass">{{ $t('btn.search') }}</el-button>
        <filtration
          :ftype="ftype"
          :msg="tacitly"
          :select-data="selectData"
          :table-header="tableHeader1"
          @seachData="handleQuerySonClass"
        />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        :table-data="tableData"
        :table-header="tableHeader"
        sortable
        table-selection
        @selectionChange="selectionDatas"
        @clickRow="particulars"
      >
        <template v-slot:tagSlot="col">
          <el-link type="primary" @click="particulars(col.scope.row)">{{ col.scope.row.fbillno }}</el-link>
        </template>
        <template v-slot:billSlot="clo">
          <el-tag v-if="clo.scope.row.fforbidStatus==='否'">{{ clo.scope.row.fforbidStatus }}</el-tag>
          <el-tag v-else type="danger">{{ clo.scope.row.fforbidStatus }}</el-tag>
        </template>
        <template v-slot:btnState="col">
          <el-tag :type="col.scope.row.stateColor">{{ col.scope.row.fdocumentstatus }}</el-tag>
        </template>
      </jc-table>
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        v-show="total > 0"
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        @pagination="handleGetUntreated"
      />
    </div>
  </div>
</template>

<script>
import {
  queryCashFlowProjectList, submitCashFlowProject,
  notsubmitCashFlowProject, reviewCashFlowProject,
  notreviewCashFlowProject, deleteCashFlowProject
} from '@/api/basicManagement/cashFlow'
import search from '@/components/Search'
import searData from '@/components/Search/mixin'
import { toMxBmina, Disable } from '@/components/ToMxamineState'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import filtration from '@/components/filtration'
import jcButton from '@/components/Button'
export default {
  components: {
    search,
    filtration,
    jcButton
  },
  mixins: [searData],
  data() {
    return {
      ftype: 94,
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      // 表头
      tableData: [], // 销售数据
      tableHeader: [
        { label: this.$t('shop.fnumber'), type: 'tag', prop: 'fbillno', width: '120' },
        { label: this.$t('shop.name1'), prop: 'fname' },
        { label: this.$t('market.descrip'), prop: 'fdescription' },
        { label: '类别属性', prop: 'fbilltyper' },
        { label: '方向', prop: 'fdirectionor' },
        { label: this.$t('market.documentstatus'), type: 'state', prop: 'fdocumentstatus', width: '80' },
        { label: this.$t('market.forbidStatu'), type: 'bill', prop: 'fforbidStatus', width: '80' }
      ],
      tableHeader1: [
        { label: this.$t('market.creator'), prop: 'fcreator' },
        { label: this.$t('market.createDate'), prop: 'fcreateDate' }
      ]
    }
  },
  created() {
    this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader.concat(this.tableHeader1)
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else if (this.val && this.val.length > 0) {
          const list = this.val
          const data = formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '现金流量项目')
        } else {
          queryCashFlowProjectList({
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.list.map(item => {
                return (toMxBmina(item), Disable(item))
              })
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '现金流量项目')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取供应商列表数据
    async handleGetUntreated(ev) {
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = { pageNum: this.currentPage, pageSize: this.size, search: this.searCollData.concat(this.details) }
      const { data: RES } = await queryCashFlowProjectList(DATA)
      this.tableData = RES.list.map(item => {
        return (toMxBmina(item), Disable(item))
      })
      this.total = RES.total
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    selectionDatas(val) {
      this.selectFbi = []
      this.selectids = []
      this.val = val
      this.val.forEach(res => {
        res.fbillno && this.selectFbi.push(res.fbillno)
        this.selectids.push(res.fid)
      })
    },
    // 公共函数
    toExamineFun(api, ev) {
      const DATA = {
        Numbers: this.selectFbi,
        ids: ev === 1 ? this.selectids : []
      }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.handleGetUntreated()
        this.selectFbi = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 审核
    Retrial(ev) {
      const api = ev === 1 ? reviewCashFlowProject : notreviewCashFlowProject
      this.toExamineFun(api)
    },
    // 提交
    materSub(ev) {
      const api = ev === 1 ? submitCashFlowProject : notsubmitCashFlowProject
      this.toExamineFun(api)
    },
    // 删除
    delCustom() {
      this.$confirm('确认要删除么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        const api = deleteCashFlowProject
        this.toExamineFun(api, 1)
      })
    },
    // 新增
    addMateril() {
      this.$router.push({ path: '/cashFlowAdd' })
    },
    // 现金流量详情
    particulars(ev) {
      const id = ev.fid
      this.$router.push({ path: `/cashFlowDetail/${id}` })
    },
    // 自定义列表
    customHandle(ev) {
      this.tableHeaders = []
      if (ev.header) {
        ev.header.forEach(item => {
          this.tableHeader1.forEach(res => {
            if (res.label === item) {
              this.tableHeaders.push(res)
            }
          })
        })
        this.tableHeader = this.tableHeaders
      } else if (ev.fname === '缺省方案') {
        this.tableHeader = this.oldTabHeader
      } else {
        this.tableHeader = this.tableHeader1
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.content {
  @include listBom;
  .header{
    position:relative;
    .header-name{
      width: 100%;
    }
    .btn{
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
</style>
